//
//  WuMainViewController.swift
//  microBlog
//
//  Created by Wu YJ on 15/11/22.
//  Copyright © 2015年 Wu YJ. All rights reserved.
//

import UIKit

class WuMainViewController: UITabBarController {

    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        
        //KVC
        let newTabBar = WuTabBar()
        setValue(newTabBar, forKey: "tabBar")

        tabBar.tintColor = UIColor.orangeColor()
    
        //home
        let homeVC = WuHomeViewController()
        addChileVC(homeVC, title: "主页", imageName: "tabbar_home")
        
        
        //message
        let message = WuMessageViewController()
        addChileVC(message, title: "消息", imageName: "tabbar_message_center")
        
        //discover
        let discover = WuMessageViewController()
        addChileVC(discover, title: "发现", imageName: "tabbar_discover")
        
        //profile
        let profile = WuMessageViewController()
        addChileVC(profile, title: "我", imageName: "tabbar_profile")
    }
    
    override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated)
        let width = tabBar.bounds.size.width / 5
        let frame = CGRect(x: 2 * width - 5, y: 0, width:width + 10 , height: tabBar.bounds.height)
        WuBtn.frame = frame
    }
    
    
    func addChileVC(controller: UIViewController , title: String , imageName: String)
    {
        controller.title = title
        controller.tabBarItem.image = UIImage(named:imageName)

        
        addChildViewController(UINavigationController(rootViewController: controller))
    }
    
    /**
    *  懒加载按钮  设置按钮idea背景图片和图片 ， 将按钮添加到tabBar上，为按钮添加点击事件
    */
    lazy var WuBtn : UIButton =
    {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        self.tabBar.addSubview(button)
        
        button.addTarget(self, action: "wuBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    
    func wuBtnClick()
    {
        print("你点击了中间加的按钮")
    }
    
    
}

